package org.kms.Controllers;

import org.kms.dao.skshDao;
import org.kms.pojo.Sksh;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Sksh")
@CrossOrigin(value = "*")
public class skshController {
    @Autowired
    private skshDao skshDao;

    @RequestMapping("/index")
    @ResponseBody
    public ResponseResult<List<Sksh>> index(Sksh sksh)throws Exception{
        sksh.setSfjf("否");
        List<Sksh> c = skshDao.getConditionList(sksh);
        return new ResponseResult<>(c);
    }
    @RequestMapping("/pd")
    @ResponseBody
    public int pd(String start,String end,Integer pmId)throws Exception{
        return skshDao.getbytimeandpmid(start,end,pmId);
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Sksh sksh)throws Exception{
        skshDao.add(sksh);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(Sksh sksh)throws Exception{
        skshDao.update(sksh);
        return ResponseResult.createSuccessResult();
    }
    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }
}
